package com.zz.dialog

import android.annotation.SuppressLint
import android.content.DialogInterface
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.zz.dialog.databinding.ActivityMainBinding
import com.zz.dialog.databinding.DialogLoadingProgressBinding
import com.zz.dialog.databinding.DialogVersionUpgrdeBinding
import com.zz.dialog.databinding.DialogVersionUpgrdeStrongBinding
import com.zz.xdialog.XDialog
import com.zz.xdialog.listener.OnBindViewListener

class MainActivity : AppCompatActivity() {
    var currProgress: Int = 0
    var xDialog: XDialog? = null
    lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    fun useDialog(view: View) {
        XDialog.Builder(supportFragmentManager)
            .setViewBinding(DialogVersionUpgrdeBinding::class.java) //设置弹窗展示的xml布局
            .setWidth(600) //设置弹窗宽度(px)
            .setHeight(800) //设置弹窗高度(px)
            .setScreenWidthAspect(this, 0.8f) //设置弹窗宽度(参数aspect为屏幕宽度比例 0 - 1f)
            .setScreenHeightAspect(this, 0.3f) //设置弹窗高度(参数aspect为屏幕宽度比例 0 - 1f)
            .setGravity(Gravity.CENTER) //设置弹窗展示位置
            .setTag("DialogTest") //设置Tag
            .setDimAmount(0.8f) //设置弹窗背景透明度(0-1f)
            .setCancelableOutside(false) //弹窗在界面外是否可以点击取消
            .setDialogAnimationRes(R.style.animate_dialog) //设置弹窗动画
            .setOnDismissListener({ //弹窗隐藏时回调方法
                Toast.makeText(this@MainActivity, "弹窗消失回调", Toast.LENGTH_SHORT).show()
            })
            .setOnBindViewListener(object : OnBindViewListener<DialogVersionUpgrdeBinding> {
                override fun onBind(binding: DialogVersionUpgrdeBinding, xDialog: XDialog) {
                    binding.textView5.setText("发现新版本1.1")
                    binding.tvUpgradeContent.setText("版本升级内容,升级内容")
                    binding.tvCancel.setOnClickListener {
                        xDialog.dismiss()
                    }
                    binding.tvConfirm.setOnClickListener {
                        Toast.makeText(this@MainActivity, "confirm clicked", Toast.LENGTH_SHORT).show()
                        xDialog.dismiss()
                    }
                    binding.textView5.setOnClickListener {
                        Toast.makeText(this@MainActivity, "title clicked", Toast.LENGTH_SHORT).show()
                        binding.textView5.setText("Title点击了")
                    }
                }
            })
            .setOnKeyListener { dialog, keyCode, event ->
                Toast.makeText(this@MainActivity, "按键 keyCode:$keyCode", Toast.LENGTH_SHORT).show()
                false
            }
            .create() //创建TDialog
            .show() //展示
    }

    fun upgradeDialog(view: View) {
        XDialog.Builder(supportFragmentManager)
            .setViewBinding(DialogVersionUpgrdeBinding::class.java)
            .setScreenWidthAspect(this, 0.7f)
            .setDialogAnimationRes(R.style.animate_dialog_scale)
            .setOnBindViewListener(object : OnBindViewListener<DialogVersionUpgrdeBinding> {
                override fun onBind(binding: DialogVersionUpgrdeBinding, xDialog: XDialog) {
                    binding.tvConfirm.setOnClickListener {
                        Toast.makeText(this@MainActivity, "开始下载新版本apk文件", Toast.LENGTH_SHORT).show()
                        xDialog.dismiss()
                    }
                    binding.tvCancel.setOnClickListener {
                        xDialog.dismiss()
                    }
                }

            })
            .setCancelableOutside(false)
            .create()
            .show();
    }

    fun upgradeDialogStrong(view: View) {
        XDialog.Builder(supportFragmentManager)
            .setViewBinding(DialogVersionUpgrdeStrongBinding::class.java)
            .setScreenWidthAspect(this, 0.7f)
            .setCancelableOutside(false)
            .setDialogAnimationRes(R.style.animate_dialog_scale)
            .setOnBindViewListener(object : OnBindViewListener<DialogVersionUpgrdeStrongBinding> {
                override fun onBind(binding: DialogVersionUpgrdeStrongBinding, xDialog: XDialog) {
                    binding.tvConfirm.setOnClickListener {
                        Toast.makeText(this@MainActivity, "开始下载新版本apk文件", Toast.LENGTH_SHORT).show()
                        xDialog.dismiss()
                    }
                }
            })
            .setOnKeyListener(DialogInterface.OnKeyListener { dialog, keyCode, event ->
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    Toast.makeText(this, "返回健无效，请强制升级后使用", Toast.LENGTH_SHORT).show()
                    return@OnKeyListener true
                }
                false
            })
            .create()
            .show()
    }

    var mBinding: DialogLoadingProgressBinding? = null
    fun progressDialog(view: View?) {
        xDialog = XDialog.Builder(supportFragmentManager)
            .setViewBinding(DialogLoadingProgressBinding::class.java)
            .setScreenWidthAspect(this, 0.8f)
            .setCancelableOutside(false)
            .setOnBindViewListener(object : OnBindViewListener<DialogLoadingProgressBinding> {
                override fun onBind(binding: DialogLoadingProgressBinding, dialog: XDialog) {
                    this@MainActivity.mBinding = binding
                }
            })
            .setOnDismissListener {
                handler.removeMessages(WHAT_PROGRESS)
                currProgress = 0
            }
            .create()
            .show()
        handler.sendEmptyMessage(WHAT_PROGRESS)
    }


    private val handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                WHAT_LOADING -> {
                    xDialog?.dismiss()
                    return
                }

                WHAT_PROGRESS -> {
                    currProgress += 1
                    mBinding?.progressBar?.setProgress(currProgress)
                    mBinding?.tvProgress?.setText("progress:${currProgress/10}/100")
                    if (currProgress >= 1000) {
                        currProgress = 0
                        xDialog?.dismiss()
                        xDialog = null
                    } else {
                        sendEmptyMessageDelayed(WHAT_PROGRESS, 1)
                    }
                    return
                }
            }
        }
    }

    companion object {
        const val WHAT_LOADING: Int = 0
        const val WHAT_PROGRESS: Int = 1
    }
}
